package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateUserGoodsPic
//@description: 创建UserGoodsPic记录
//@param: userGoodsPic model.UserGoodsPic
//@return: err error

func CreateUserGoodsPic(userGoodsPic model.UserGoodsPic) (err error) {
	err = global.GVA_DB.Create(&userGoodsPic).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserGoodsPic
//@description: 删除UserGoodsPic记录
//@param: userGoodsPic model.UserGoodsPic
//@return: err error


func DeleteUserGoodsPic(userGoodsPic model.UserGoodsPic) (err error) {
	err = global.GVA_DB.Delete(&userGoodsPic).Error
	return err
}

func DeleteUserGoodsPicWithGoodsId(goodsId uint)(err error) {
	err = global.GVA_DB.Table(`user_goods_pic`).Delete(&[]model.UserGoodsPic{},`goods_id=?`,goodsId).Error
	return err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserGoodsPicByIds
//@description: 批量删除UserGoodsPic记录
//@param: ids request.IdsReq
//@return: err error

func DeleteUserGoodsPicByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserGoodsPic{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateUserGoodsPic
//@description: 更新UserGoodsPic记录
//@param: userGoodsPic *model.UserGoodsPic
//@return: err error

func UpdateUserGoodsPic(userGoodsPic model.UserGoodsPic) (err error) {
	err = global.GVA_DB.Save(&userGoodsPic).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserGoodsPic
//@description: 根据id获取UserGoodsPic记录
//@param: id uint
//@return: err error, userGoodsPic model.UserGoodsPic

func GetUserGoodsPic(id uint) (err error, userGoodsPic model.UserGoodsPic) {
	err = global.GVA_DB.Where("id = ?", id).First(&userGoodsPic).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserGoodsPicInfoList
//@description: 分页获取UserGoodsPic记录
//@param: info request.UserGoodsPicSearch
//@return: err error, list interface{}, total int64

func GetUserGoodsPicInfoList(info request.UserGoodsPicSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.UserGoodsPic{})
    var userGoodsPics []model.UserGoodsPic
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&userGoodsPics).Error
	return err, userGoodsPics, total
}